package cn.org.hentai.jtt1078.subscriber;

import cn.org.hentai.jtt1078.codec.MP3Encoder;
import cn.org.hentai.jtt1078.flv.AudioTag;
import cn.org.hentai.jtt1078.flv.FlvAudioTagEncoder;
import cn.org.hentai.jtt1078.flv.FlvEncoder;
import cn.org.hentai.jtt1078.util.*;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.LinkedList;

public class RTMPPublisher extends Thread
{
    static Logger logger = LoggerFactory.getLogger(RTMPPublisher.class);

    String tag = null;
    Process process = null;

    public RTMPPublisher(String tag)
    {
        this.tag = tag;
    }

    @Override
    public void run()
    {
        InputStream stderr = null;
        int len = -1;
        byte[] buff = new byte[512];
        boolean debugMode = "on".equalsIgnoreCase(Configs.get("debug.mode"));

        try
        {
//            String rtmpUrl = Configs.get("rtmp.url").replaceAll("\\{TAG\\}", tag);
//            String cmd = String.format("%s -i http://localhost:%d/video/%s -vcodec copy -acodec aac -f flv %s",
//                        Configs.get("ffmpeg.path"),
//                        Configs.getInt("server.http.port", 3333),
//                        tag,
//                        rtmpUrl
//                    );
            String yyyyMMddHHmmss = tag + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
/*D:\soft\ffmpeg-master-latest-win64-gpl\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe -i "http://localhost:3333/video/013800138999-2" -vcodec copy -acodec aac -f flv -t 10 out.h264

D:\soft\ffmpeg-master-latest-win64-gpl\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe -i "http://localhost:3333/video/013800138999-2" -c:v libx264 -c:a aac -strict experimental -f flv output.flv


D:\soft\ffmpeg-master-latest-win64-gpl\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe -i "http://localhost:3333/video/013800138999-2" -f image2 -r 1  output.jpg

D:\soft\ffmpeg-master-latest-win64-gpl\ffmpeg-master-latest-win64-gpl\bin\ffmpeg.exe -i "http://localhost:3333/video/013800138999-2" -vcodec mpeg4 D:\output.mp4*/
            //ffmpeg 生成本地 mp4文件
            String cmd = String.format("%s -i http://localhost:%d/video/%s -vcodec mpeg4  D:\\%s.mp4",
                        Configs.get("ffmpeg.path"),
                        Configs.getInt("server.http.port", 3333),
                        tag,yyyyMMddHHmmss
                    );
//            String cmdImg = String.format("%s -i D:\\%s.mp4 -f image2 -r 1  D:\\%s.jpg",
//                    Configs.get("ffmpeg.path"),
//                    yyyyMMddHHmmss,yyyyMMddHHmmss
//            );
            logger.info("Execute: {}", cmd);
            process = Runtime.getRuntime().exec(cmd);
            stderr = process.getErrorStream();
            while ((len = stderr.read(buff)) > -1)
            {
                if (debugMode) System.out.print(new String(buff, 0, len));
            }
//            process = Runtime.getRuntime().exec(cmdImg);
            stderr = process.getErrorStream();
            while ((len = stderr.read(buff)) > -1)
            {
                if (debugMode) System.out.print(new String(buff, 0, len));
            }
            logger.info("Process FFMPEG exited...");
        }
        catch(Exception ex)
        {
            logger.error("publish failed", ex);
        }
    }

    public void close()
    {
        try { if (process != null) process.destroyForcibly(); } catch(Exception e) { }
    }
}